import axios from 'axios'
import vue from 'vue'
const service = axios.create({
    headers: {
        'Content-Type': "application/json;charset=utf-8"
    },
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 10000000
})
// request拦截器
service.interceptors.request.use(
    config => {
        const isToken = localStorage.getItem("token");//(res.headers || {}).isToken === false
        if (isToken) {
            config.headers['Authorization'] = isToken// + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
        }
        return config
    },
    error => {
        Promise.reject(error)
    }
)

// 响应拦截器
service.interceptors.response.use(res => {
        const code = res.data.code;
        if (code != 0) {
            if(code === -9001){
                localStorage.removeItem('token');
            }
            //alert(res.data.msg);
            //Toast.fail(res.data.msg);
            return res.data;
        } else {
            return res.data
        }
    },
    error => {
        alert(error)
        return Promise.reject(error)
    }
)

export default service
